package com.itheima.bigeventt.controller;

import com.itheima.bigeventt.dto.pwdDto;
import com.itheima.bigeventt.pojo.Result;
import com.itheima.bigeventt.pojo.User;
import com.itheima.bigeventt.service.UserService;
import com.itheima.bigeventt.utils.JwtUtil;
import com.itheima.bigeventt.utils.Md5Util;
import com.itheima.bigeventt.utils.ThreadLocalUtil;
import org.hibernate.validator.constraints.URL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping("/register")
    public Result register(String username, String password) {
        User u=userService.findByUserName(username);
        if(u==null) {
            userService.register(username,password);
            return Result.success("创建成功");
        }else {
            return Result.error("用户名已占用");
        }
    }

    @PostMapping("/login")
    public Result login(String username, String password) {
        User u=userService.findByUserName(username);
        if(u==null) {
            return Result.error("用户名不存在");
        }else {
            if(Md5Util.getMD5String(password).equals(u.getPassword())) {
                Map<String,Object> claims=new HashMap<>();
                claims.put("username",username);
                claims.put("password",password);
                String token = JwtUtil.genToken(claims);
                redisTemplate.opsForValue().set("token",token,1, TimeUnit.HOURS);
                return Result.success(token);
            }else{
                return Result.error("密码错误");
            }
        }
    }

    @GetMapping("/userInfo")
    public Result<User> getUserInfo(@RequestHeader(name="Authorization") String token) {
//        Map<String,Object> map= JwtUtil.parseToken(token);
//        String username = (String) map.get("username");
        Map<String,Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        User user = userService.findByUserName(username);
        return Result.success(user);
    }

    @PutMapping("/update")
    public Result update(@RequestBody @Validated User user) {
        User id = userService.getById(user.getId());
        if(id==null) {
            return Result.error("不存在该用户");
        }else{
            userService.updateById(user);
            return Result.success();
        }
    }

    @PatchMapping("/updateAvatar")
    public Result updateAvatar(@RequestParam @URL String avatarUrl) {
        Map<String,Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        User user = userService.findByUserName(username);
        user.setUserPic(avatarUrl);
        userService.updateById(user);
        return Result.success();
    }

    @PatchMapping("/updatePwd")
    public Result updatePwd(@RequestBody pwdDto pwdDto ,@RequestHeader("Authorization") String token) {
        if (!StringUtils.hasLength(pwdDto.oldpwd)||!StringUtils.hasLength(pwdDto.newpwd)||!StringUtils.hasLength(pwdDto.repwd)){
            return Result.error("缺少必要参数");
        }
        Map<String,Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        User user = userService.findByUserName(username);
        if (Md5Util.getMD5String(pwdDto.oldpwd).equals(user.getPassword())){
            if (pwdDto.newpwd.equals(pwdDto.repwd)){
                user.setPassword(Md5Util.getMD5String(pwdDto.newpwd));
                userService.updateById(user);
                redisTemplate.delete(token);
                return Result.success("修改成功");
            }
            else return Result.error("两次新密码输入不一致");
        }else return Result.error("旧密码错误");
    }
}
